'use strict';

const Controller = require('egg').Controller;

class DashboardController extends Controller {
  /**
   * 获取仪表盘统计数据
   * GET /api/dashboard/stats
   */
  async getStats() {
    const { ctx, service } = this;

    try {
      const stats = await service.dashboard.getStats();
      ctx.body = {
        success: true,
        data: stats
      };
    } catch (error) {
      ctx.logger.error('获取仪表盘统计失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取统计数据失败: ' + error.message
      };
    }
  }

  /**
   * 获取热门城市排行（按推荐数）
   * GET /api/dashboard/top-cities
   */
  async getTopCities() {
    const { ctx, service } = this;

    try {
      const topCities = await service.dashboard.getTopCities();
      ctx.body = {
        success: true,
        data: topCities
      };
    } catch (error) {
      ctx.logger.error('获取热门城市排行失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取热门城市排行失败: ' + error.message
      };
    }
  }

  /**
   * 获取平台活跃度趋势（按月统计）
   * GET /api/dashboard/activity-trend
   */
  async getActivityTrend() {
    const { ctx, service } = this;

    try {
      const trend = await service.dashboard.getActivityTrend();
      ctx.body = {
        success: true,
        data: trend
      };
    } catch (error) {
      ctx.logger.error('获取平台活跃度趋势失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取平台活跃度趋势失败: ' + error.message
      };
    }
  }

  /**
   * 获取用户增长趋势（按周统计）
   * GET /api/dashboard/user-growth
   */
  async getUserGrowth() {
    const { ctx, service } = this;

    try {
      const growth = await service.dashboard.getUserGrowth();
      ctx.body = {
        success: true,
        data: growth
      };
    } catch (error) {
      ctx.logger.error('获取用户增长趋势失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取用户增长趋势失败: ' + error.message
      };
    }
  }

  /**
   * 获取内容分类统计
   * GET /api/dashboard/content-category
   */
  async getContentCategory() {
    const { ctx, service } = this;

    try {
      const category = await service.dashboard.getContentCategory();
      ctx.body = {
        success: true,
        data: category
      };
    } catch (error) {
      ctx.logger.error('获取内容分类统计失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '获取内容分类统计失败: ' + error.message
      };
    }
  }
}

module.exports = DashboardController;

